define(['./../initial'], function() {
    var app = angular.module('App.home');
    app.factory('LocationPoints', ['$q', '$http', '$rootScope',
        function($q, $http, $rootScope) {
            var locationPoints = function() {
                this.allTimes = [];
                this.recentChanges = null;
                this.points = [];
                this.location = "";
                this.autoSurveyPrjId = "";
                this.autoSurveyPrjName = "";
                this.isAutoSurveyPrj = false;
                this.autoSurveyPrjOnline = false;
                this.selectedDates = []; //默认选择的兴趣时间列表
                this.isDesignMode = false;
                this.isRingNumberConfig = true; //是否配置了里程和环号,并且可以在地图上显示
            };


            locationPoints.prototype.getRecentChanges = function(prjId, monitorType) {
                var defer = $q.defer();
                var ll = this;
                this.isDesignMode = false; //设置查询设计值为false
                if (ll.recentChanges) {
                    ll.recentChanges.allTimes = ll.allTimes;
                    defer.resolve(ll.recentChanges);
                } else {
                    var url = '/recentChanges/' + prjId + "/" + monitorType + "/" + Base64.encodeURI(ll.location);

                    $http({
                        url: url,
                        method: 'get'
                    }).success((function(pl) {
                        return function(data) {
                            pl.recentChanges = data;
                            pl.allTimes = data.allTimes;
                            $rootScope.$broadcast('allTimes', pl.allTimes);
                            //判断报警
                            pl.recentChanges.curValues.forEach(function(item, index) {
                                if (Math.abs(item.value) >= 5) {
                                    pl.points[index].alert = true;
                                } else {
                                    pl.points[index].alert = false;
                                }
                            });
                            defer.resolve(data);
                        };
                    })(ll));
                }
                return defer.promise;
            };
            locationPoints.prototype.getRecentFuzzyChanges = function(prjId, monitorType, clickDates, location, fuzzy) {

                fuzzy = fuzzy || 0;
                location = location || this.location;

                //default params
                var defer = $q.defer();
                var ll = this;
                $http({
                    url: "/recentChanges/" + prjId.toString() + "/" + monitorType + "/" + Base64.encodeURI(ll.location) + "/" + clickDates[0] + "/" + clickDates[1] + "/" + fuzzy,
                    method: 'get'
                }).success((function(pl) {
                    return function(data) {
                        defer.resolve(data);
                    };
                })(ll));
                return defer.promise;
            };

            locationPoints.prototype.getRecent2Changes = function(prjId, monitorType, clickDates, location, fuzzy) {

                fuzzy = fuzzy || 0;
                location = location || this.location;

                //default params
                var defer = $q.defer();
                var ll = this;
                $http({
                    url: "/recentChangesByTimeList/" + prjId.toString() + "/" + monitorType + "/" + Base64.encodeURI(ll.location) + "/" + clickDates.join(",") + "/" + fuzzy + "/" + this.isDesignMode,
                    method: 'get'
                }).success((function(pl) {
                    return function(data) {
                        defer.resolve(data);
                    };
                })(ll));
                return defer.promise;
            };


            return locationPoints;
        }
    ]);
});
